from flask import Blueprint, json,request,jsonify
from comm import get_data
from db import db1
from jwtc import my_jwt
import time
useradmin=Blueprint('useradmin',__name__)
@useradmin.route('/login',methods=['GET','POST'])
def login():
    if request.method=='POST':
        data=get_data()
        name=request.args.get('name')
        password=request.args.get('password')
        sql="select * from usergl where name='%s' and password = '%s'"%(name,password)
        res=db1.find_one(sql)
        if res:
            # 没过期，重新生成token
            data={  
                'data':{
                    "phone":data['phone'],
                    "id":data['id'],
                    "iat":time.time(),
                    "exp":3600
                }
            }
            newtoken=my_jwt.jwt_en(data)
            data1={
                'data':{
                    "iat":time.time(),
                    "exp":7200
                }
            }
            newretoken=my_jwt.jwt_en(data1)
            return jsonify({'code':200,'token':newtoken,'retoken':newretoken})

        else:
            return jsonify({'mes':'用户名或密码不存在','code':204})



